/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.mach.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import lombok.AllArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springblade.core.excel.util.ExcelUtil;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.mach.excel.PurchasedPartsExcel;
import org.springblade.mach.excel.PurchasedPartsImporter;
import org.springblade.mach.excel.StandardPartsExcel;
import org.springblade.mach.utils.ExcelUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RequestParam;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springblade.mach.entity.PurchasedParts;
import org.springblade.mach.vo.PurchasedPartsVO;
import org.springblade.mach.wrapper.PurchasedPartsWrapper;
import org.springblade.mach.service.IPurchasedPartsService;
import org.springblade.core.boot.ctrl.BladeController;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *  控制器
 *
 * @author LY
 * @since 2021-04-22
 */
@RestController
@AllArgsConstructor
@RequestMapping("data-purchased/purchasedparts")
@Api(value = "", tags = "接口")
public class PurchasedPartsController extends BladeController {

	private final IPurchasedPartsService purchasedPartsService;
	/**
	 *导入采购件
	 */
	@PostMapping("/import-purchased-parts")
	public R importStandardParts(MultipartFile file, Integer isCovered){
		PurchasedPartsImporter importer = new PurchasedPartsImporter(purchasedPartsService , isCovered == 1);
		ExcelUtil.save(file, importer, PurchasedPartsExcel.class);
		return R.data("操作成功");
	}

	@GetMapping("export-template")
	public void exportStandardParts(HttpServletResponse response){
		List<PurchasedPartsExcel> list = new ArrayList<>();
		ExcelUtil.export(response,"国标件物料信息导入模板","sheet1", list, PurchasedPartsExcel.class);
	}



	/**
	 * 详情
	 */
	@GetMapping("/detail")
	@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入purchasedParts")
	public R<PurchasedPartsVO> detail(PurchasedParts purchasedParts) {
		PurchasedParts detail = purchasedPartsService.getOne(Condition.getQueryWrapper(purchasedParts));
		return R.data(PurchasedPartsWrapper.build().entityVO(detail));
	}

	/**
	 * 分页
	 */
	@GetMapping("/list")
	@ApiOperationSupport(order = 2)
	@ApiOperation(value = "分页", notes = "传入purchasedParts")
	public R<IPage<PurchasedPartsVO>> list(@RequestParam Map<String,Object> purchasedParts, Query query) {
		IPage<PurchasedParts> pages = purchasedPartsService.page(Condition.getPage(query),
			Condition.getQueryWrapper(purchasedParts,PurchasedParts.class).
				lambda().orderByDesc(PurchasedParts::getCreateTime));
		return R.data(PurchasedPartsWrapper.build().pageVO(pages));
	}


	/**
	 * 自定义分页
	 */
	@GetMapping("/page")
	@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入purchasedParts")
	public R<IPage<PurchasedPartsVO>> page(PurchasedPartsVO purchasedParts, Query query) {
		IPage<PurchasedPartsVO> pages = purchasedPartsService.selectPurchasedPartsPage(Condition.getPage(query), purchasedParts);
		return R.data(pages);
	}

	/**
	 * 新增
	 */
	@PostMapping("/save")
	@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入purchasedParts")
	public R save(@Valid @RequestBody PurchasedParts purchasedParts) {
		return R.status(purchasedPartsService.save(purchasedParts));
	}

	/**
	 * 修改
	 */
	@PostMapping("/update")
	@ApiOperationSupport(order = 5)
	@ApiOperation(value = "修改", notes = "传入purchasedParts")
	public R update(@Valid @RequestBody PurchasedParts purchasedParts) {
		return R.status(purchasedPartsService.updateById(purchasedParts));
	}

	/**
	 * 新增或修改
	 */
	@PostMapping("/submit")
	@ApiOperationSupport(order = 6)
	@ApiOperation(value = "新增或修改", notes = "传入purchasedParts")
	public R submit(@Valid @RequestBody PurchasedParts purchasedParts) {
		return R.status(purchasedPartsService.saveOrUpdate(purchasedParts));
	}


	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ApiOperationSupport(order = 7)
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		return R.status(purchasedPartsService.deleteLogic(Func.toLongList(ids)));
	}


}
